package com.example.controller;



import com.example.Repository.UserRepository;
import com.example.pojo.Result;
import com.example.pojo.dto.LoginDto;
import com.example.pojo.dto.RegisterDto;
import com.example.pojo.vo.UserVo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author crt
 */
@RestController
@RequestMapping("user")
@Validated
public class UserController {

    @Resource
    private UserRepository userRepository;

    @PostMapping("/register")
    public Result<String> register(@Valid @RequestBody RegisterDto registerDto){

        return userRepository.register(registerDto);
    }

    @PostMapping("/login")
    public Result<String> login(@Valid @RequestBody LoginDto loginDto) throws Exception {

        return userRepository.login(loginDto);
    }


    @GetMapping("/userInfo")
    public Result<UserVo> getUserInfo(@Pattern(regexp = "^\\S{5,16}$",message = "用户名需要再5到16位") @RequestParam String username){
        return userRepository.getUserInfo(username);
    }

}
